package com.wiscamp.ninechapters.problems.adapter.resources.types;

import com.wiscamp.ninechapters.problems.application.dto.commands.types.CreateQuestionTypeCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.types.UpdateQuestionTypeCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.types.QuestionTypeVO;
import com.wiscamp.ninechapters.problems.application.services.QuestionTypeAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.application.command.DisableCommand;
import cube.ddd.application.command.EnableCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 问题类型服务端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")
@Tag(name = "题库-问题类型信息")
public class QuestionTypeController extends BaseController {

    private final QuestionTypeAppService questionTypeAppService;

    public QuestionTypeController(QuestionTypeAppService questionTypeAppService) {
        this.questionTypeAppService = questionTypeAppService;
    }


    /**
     * 通过问题类型编号获取问题类型
     *
     * @param request
     * @param response
     * @param typeId
     * @return
     */
    @GetMapping("QuestionTypes/{typeId}")
    @Operation(summary = "通过问题类型编号获取问题类型")
    @HandleResourceException
    public RestResponse<QuestionTypeVO> getQuestionTypeById(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                            @PathVariable("typeId") long typeId) {
        return ResourceUtils.getById(request, response, typeId, questionTypeAppService::getQuestionType);
    }

    /**
     * 通过机构编号和问题类型代码获取问题类型
     *
     * @param typeCode
     * @return
     */
    @GetMapping("QuestionTypes/Code/{typeCode}")
    @Operation(summary = "通过机构编号和问题类型代码获取问题类型")
    @HandleResourceException
    public RestResponse<QuestionTypeVO> getQuestionTypeByCode(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                              @PathVariable("typeCode") String typeCode) {
        return ResourceUtils.getByText(request, response, typeCode, questionTypeAppService::getQuestionTypeByCode);
    }


    /**
     * 通过机构编号和问题类型名称获取问题类型
     *
     * @param typeName
     * @return
     */
    @GetMapping("QuestionTypes/Name/{typeName}")
    @Operation(summary = "通过机构编号和问题类型名称获取问题类型")
    @HandleResourceException
    public RestResponse<Collection<QuestionTypeVO>> getQuestionTypesByName(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                           @PathVariable("typeName") String typeName) {
        return ResourceUtils.getListByText(request, response, typeName, questionTypeAppService::getQuestionTypesByName);
    }

    @GetMapping("QuestionTypes/Available")
    @Operation(summary = "获取可用问题类型")
    @HandleResourceException
    public RestResponse<Collection<QuestionTypeVO>> getAvailableQuestionTypes(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        return ResourceUtils.getList(request, response, questionTypeAppService::getAvailableQuestionTypes);
    }

    /**
     * 通过问题类型编号删除问题类型
     *
     * @param command
     * @return
     */
    @PutMapping("QuestionTypes/Actions/Delete")
    @Operation(summary = "通过问题类型编号删除问题类型")
    @HandleResourceException
    public RestResponse<Boolean> deleteQuestionType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, questionTypeAppService::deleteQuestionType);
    }

    @PutMapping("QuestionTypes/Actions/Enable")
    @Operation(summary = "通过问题类型编号启用问题类型")
    @HandleResourceException
    public RestResponse<Boolean> enableQuestionType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @RequestBody EnableCommand command) {
        return ResourceUtils.enable(request, response, command, questionTypeAppService::enableQuestionType);
    }

    @PutMapping("QuestionTypes/Actions/Disable")
    @Operation(summary = "通过问题类型编号禁用问题类型")
    @HandleResourceException
    public RestResponse<Boolean> disableQuestionType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                     @RequestBody DisableCommand command) {
        return ResourceUtils.disable(request, response, command, questionTypeAppService::disableQuestionType);
    }

    /**
     * 创建问题类型
     *
     * @param command
     * @return
     */
    @PostMapping("QuestionTypes")
    @Operation(summary = "创建问题类型")
    @HandleResourceException
    public RestResponse<QuestionTypeVO> createQuestionType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @RequestBody CreateQuestionTypeCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, questionTypeAppService::createQuestionType);
    }

    /**
     * 更新问题类型
     *
     * @param command
     * @return
     */
    @PutMapping("QuestionTypes")
    @Operation(summary = "更新问题类型")
    @HandleResourceException
    public RestResponse<QuestionTypeVO> updateQuestionType(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @RequestBody UpdateQuestionTypeCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, questionTypeAppService::updateQuestionType);
    }
}
